import { getCodeAPI, loginAPI, loginType } from '@/api/login';
import {
  getUserInfoAPI,
  getUserPhotoAPI,
  getUserProfileAPI,
  updateUserProfileAPI,
} from '@/api/user';
import { setTokenInfo } from '@/utils/storage';
import { AppDispatch } from '../index';
import { ProfileType } from '../redurce/user';

export const getCodeAction = (params: string) => {
  return async () => {
    const res = await getCodeAPI(params);
    console.log('res  ----->  ', res);
  };
};

export const loginAction = (data: loginType) => {
  return async (dispatch: AppDispatch) => {
    const res = await loginAPI(data);
    console.log('res  ----->  ', res);
    dispatch({ type: 'user/login', payload: res.data });
    setTokenInfo(res.data);
  };
};

// 获取用户个人信息
export const getUserInfoAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserInfoAPI();
    dispatch({ type: 'save/userInfo', payload: res.data });
    console.log('res  ----->  ', res);
  };
};

export const getUserProfileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserProfileAPI();
    // 4.dispatch调用逻辑
    dispatch({ type: 'save/userProfile', payload: res.data });
    console.log('res  ----->  ', res);
  };
};

export const getUserPhotoAction = (data: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserPhotoAPI(data);
    console.log('res  ----->  ', res);
    dispatch({ type: 'save/updatePhoto', payload: res.data });
  };
};

export const updateUserAction = (data: Partial<ProfileType>) => {
  return async (dispatch: AppDispatch) => {
    const res = await updateUserProfileAPI(data);
    console.log('res  ----->  ', res);
    dispatch({ type: 'save/updatePhoto', payload: data });
  };
};
